<?php

class Website extends FaceintegrationBaseModel {

    public $main_image;
    public $sub_image;

    public static function getElementById($nid) {
        $node = node_load($nid);
        if ($node !== FALSE)
            return Website::toModel($node);
        return NULL;
    }

    public static function getElementsByCategory($tid = NULL, $from = 0, $limit = 3) {
        if ($tid == NULL) {
            $query = new EntityFieldQuery();
            $queryResult = $query->entityCondition('entity_type', 'node')
                    ->propertyCondition('type', 'website')
                    ->propertyOrderBy('created', 'DESC')
                    ->range($from, $limit)
                    ->execute();
        }
        $result = array();
        if (isset($queryResult['node'])) {
            foreach ($queryResult['node'] as $key => $value) {
                $model = Website::getElementById($key);
                if ($model != NULL)
                    $result[] = $model;
            }
        }
        return $result;
    }

    public static function toModel($node) {
        $model_website = new Website();
        FaceintegrationBaseModel::initialize($node, $model_website);
        $main_image = field_get_items('node', $node, 'field_website_main_image');
        $sub_image= field_get_items('node', $node, 'field_website_sub_image');
        if (count($main_image) > 0)
            $model_website->main_image = file_create_url($main_image[0]['uri']);
            $model_website->sub_image = file_create_url($sub_image[0]['uri']);
         
        return $model_website;
    }
    public static function selectContent_type($type){
        $query = db_select('field_revision_field_website_main_image', 'fmi')
                        ->condition('fmi.bundle', $type)
                        ->fields('fmi', array('field_website_main_image_fid'))
                        ->execute()->fetchField();
        return $query;
    }
    public static function selectSub_image($fid){
        $query_image= db_select('file_managed', 'fm')
                        ->condition('fm.fid', $fid)
                        ->fields('fm', array('uri'))
                        ->execute()->fetchField();
        return $query_image;
    }

}

?>
